package com.qfedu.controller;

import com.qfedu.bean.User;
import com.qfedu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/{page}")
    public String toPage(@PathVariable("page") String page){
        System.out.println(page);
        return page;
    }
    @RequestMapping("insertPage")
    public String index() {
        return "insertPage";
    }
    @RequestMapping("select/{id}")
    @ResponseBody
    public String save(@PathVariable int id){
        return userMapper.selectById(id).toString();
    }
    @RequestMapping("/userList")
    public String userList(Model model){
        List<User> users = userMapper.selectList(null);
        model.addAttribute("users",users);
        return "userList";
    }

    @RequestMapping("/insert")
    public String save(User user){
        userMapper.insert(user);
        return "redirect:/userList";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id){
        userMapper.deleteById(id);
        return "redirect:/userList";
    }

    @GetMapping("/updatePage/{id}")
    public String updatePage(Model model, @PathVariable int id){
        User user = userMapper.selectById(id);
        model.addAttribute("user",user);
        return "updatePage";
    }

    @PostMapping("/update")
    public String updateUser(User user){
        userMapper.update(user,null);
        System.out.println("修改的用户为 ： " + user.getName());
        return "redirect:/userList";
    }

}
